package com.tulei.dangdang;


import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static java.lang.Thread.sleep;



public class DangDang {

    public static void main(String[] args) throws InterruptedException {

        List<Book> list = new ArrayList<>();
        for (int i = 1; i <5; i++) {
            list.addAll(extracted(i));
            sleep(2000);
        }
//        new ExcelHandler().writeExcel("D:\\Temp\\book.xlsx", list);
        System.out.println(list);

    }

    private static List<Book> extracted(int i) {
        List<Book> list = new ArrayList<>();
        Connection connect = Jsoup.connect("https://search.dangdang.com/?key=%CD%BC%CA%E9&act=input&page_index=" + i );
        try {
            Document document = connect.get();
            Elements elements = document.getElementsByClass("bigimg");
            Elements liElements = elements.get(0).getElementsByTag("li");

            for (Element element : liElements) {
                Book book = new Book();
                Element titleElement = element.select("a").first();
                String attr = titleElement.attr("title");
                book.setTitle(attr);


                Element pressElement = element.select("p.name a").first();
                String title = pressElement.text();
                book.setPress(title);

                // Extracting the book image URL
                Element imgElement = element.select("a img").first();
                String imageUrl = imgElement.attr("src");
                book.setImageUrl(imageUrl);

                // Extracting the current price and original price
                Elements priceElements = element.select("p.price span");
                String currentPrice = priceElements.first().text();
                String originalPrice = priceElements.get(1).text();
                if(priceElements.size() >= 3){
                    String discount = priceElements.get(2).text();
                    book.setDiscount(discount);
                }
                book.setCurrentPrice(currentPrice);
                book.setOriginalPrice(originalPrice);

                Elements commentElements = element.select("p.search_star_line a");
                if (commentElements.size() > 0) {
                    String commentNum = commentElements.first().text();
                    book.setCommentNum(commentNum);
                }

                // Extracting the publisher
                Elements publisherElements = element.select("p.search_book_author a");
                if (publisherElements.size() > 1) {
                    String author = publisherElements.get(0).text();
                    book.setAuthor(author);
                }
                if (publisherElements.size() > 0) {
                    String publisher = publisherElements.last().text();
                    book.setPublisher(publisher);
                }


                list.add(book);

            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return list;
    }

}
